import { users } from './user';

const staticComment = [
    {
        id: "1",
        content: "这篇文章讲得很透彻！",
        author: {
          email: users[1].email,
          avatar: users[1].avatar,
          name: users[1].username
        },
        created_at: Date.now() - 86400000,
        likes: 5,
        comment_total: 2,
        parent_id: ""
      },
      {
        id: "2",
        content: "我也觉得写得很好",
        author: {
          email: users[0].email,
          avatar: users[0].avatar,
          name: users[0].username 
        },
        created_at: Date.now() - 43200000,
        likes: 2,
        comment_total: 2,
        parent_id: ""
      },
      {
        id: "3",
        content: "作者确实很专业",
        author: {
          email: users[1].email,
          avatar: users[1].avatar,
          name: users[1].username 
        },
        created_at: Date.now() - 21600000,
        likes: 1,
        comment_total: 1,
        parent_id: "1"
      },
      {
        id: "4",
        content: "有几个地方不太明白",
        author: {
          email: users[0].email,
          avatar: users[0].avatar,
          name: users[0].username 
        },
        created_at: Date.now() - 72000000,
        likes: 3,
        comment_total: 2,
        parent_id: ""
      },
      {
        id: "5",
        content: "具体是哪里不明白？",
        author: {
          email: users[1].email,
          avatar: users[1].avatar,
          name: users[1].username 
        },
        created_at: Date.now() - 36000000,
        likes: 1,
        comment_total: 0,
        parent_id: "2"
      }
      ,{
        id: "6",
        content: "这个技术点很实用",
        author: {
          email: users[0].email,
          avatar: users[0].avatar,
          name: users[0].username 
        },
        created_at: Date.now() - 64800000,
        likes: 7,
        comment_total: 0,
        parent_id: "4"
      },
      {
        id: "7",
        content: "确实解决了我的问题",
        author: {
          email: users[1].email,
          avatar: users[1].avatar,
          name: users[1].username 
        },
        created_at: Date.now() - 32400000,
        likes: 3,
        comment_total: 0,
        parent_id: "1"
      },
      {
        id: "8",
        content: "同感，感谢分享",
        author: {
          email: users[0].email,
          avatar: users[0].avatar,
          name: users[0].username 
        },
        created_at: Date.now() - 16200000,
        likes: 2,
        comment_total: 0,
        parent_id: "2"
      },{
        id: "9",
        content: "期待更多相关内容",
        author: {
          email: users[1].email,
          avatar: users[1].avatar,
          name: users[1].username 
        },
        created_at: Date.now() - 57600000,
        likes: 4,
        comment_total: 0,
        parent_id: "4"
      },
      {
        id: "10",
        content: "作者会出续篇吗？",
        author: {
          email: users[0].email,
          avatar: users[0].avatar,
          name: users[0].username 
        },
        created_at: Date.now() - 28800000,
        likes: 1,
        comment_total: 0,
        parent_id: "3"
      }
]

// 静态博客文章数据
const staticBlogs = [
  {
    id: "1",
    title: 'Vue3响应式原理',
    author: {
      email: users[0].email,
      avatar: users[0].avatar,
      name: users[0].username
    },
    date: '2025-05-01 10:00:00 ',
    content: 'Vue3使用Proxy实现响应式...',
    preview: 'Vue3使用Proxy实现响应式...',
    likes: 42,
    view_count: 100,
    comments: [
    ]
  },
  {
    id: "2",
    title: 'TypeScript类型系统',
    author: {
      email: users[1].email,
      avatar: users[1].avatar,
      name: users[1].username
    },
    date: '2025-05-02 11:00:00 ',
    content: 'TypeScript的类型系统非常强大...',
    preview: 'TypeScript的类型系统非常强大...',
    likes: 35,
    view_count: 100,
    comments: [
    ]
  },
  {
    id: "3",
    title: 'React Hooks原理',
    author: {
      email: users[0].email,
      avatar: users[0].avatar,
      name: users[0].username
    },
    date: '2025-05-03 14:00:00 ',
    content: 'React Hooks的实现原理和使用技巧...',
    preview: 'React Hooks的实现原理和使用技巧...',
    likes: 29,
    view_count: 100,
    comments: [
    ]
  },
  {
    id: "4",
    title: 'Webpack配置优化',
    author: {
      email: users[1].email,
      avatar: users[1].avatar,
      name: users[1].username
    },
    date: '2025-05-04 16:00:00 ',
    content: 'Webpack打包优化的几种方法...',
    preview: 'Webpack打包优化的几种方法...',
    likes: 31,
    view_count: 100,
    comments: []
  },
  {
    id: "5",
    title: 'Node.js异步编程',
    author: {
      email: users[0].email,
      avatar: users[0].avatar,
      name: users[0].username
    },
    date: '2025-05-05 10:00:00 ',
    content: 'Node.js中的异步编程模式...',
    preview: 'Node.js中的异步编程模式...',
    likes: 27,
    view_count: 100,
    comments: []
  },
  {
    id: "6",
    title: 'CSS Grid布局',
    author: {
      email: users[1].email,
      avatar: users[1].avatar,
      name: users[1].username
    },
    date: '2025-05-06 13:00:00 ',
    content: '现代CSS Grid布局详解...',
    preview: '现代CSS Grid布局详解...',
    likes: 38,
    view_count: 100,
    comments: []
  },
  {
    id: "7",
    title: '前端安全实践',
    author: {
      email: users[0].email,
      avatar: users[0].avatar,
      name: users[0].username
    },
    date: '2025-05-07 15:00:00 ',
    content: '前端开发中的安全最佳实践...',
    preview: '前端开发中的安全最佳实践...',
    likes: 33,
    view_count: 100,
    comments: []
  },
  {
    id: "8",
    title: '微前端架构',
    author: {
      email: users[1].email,
      avatar: users[1].avatar,
      name: users[1].username
    },
    date: '2025-05-08 11:00:00 ',
    content: '微前端架构的设计与实现...',
    preview: '微前端架构的设计与实现...',
    likes: 41,
    view_count: 100,
    comments: []
  },
  {
    id: "9",
    title: 'GraphQL入门',
    author: {
      email: users[0].email,
      avatar: users[0].avatar,
      name: users[0].username
    },
    date: '2025-05-09 14:00:00 ',
    content: 'GraphQL基础概念和使用方法...',
    preview: 'GraphQL基础概念和使用方法...',
    likes: 25,
    view_count: 100,
    comments: []
  },
  {
    id: "10",
    title: 'Docker前端应用',
    author: {
      email: users[1].email,
      avatar: users[1].avatar,
      name: users[1].username
    },
    date: '2025-05-10 16:00:00 ',
    content: '使用Docker部署前端应用...',
    preview: '使用Docker部署前端应用...',
    likes: 37,
    view_count: 100,
    comments: []
  },
  {
    id: "11",
    title: '前端测试策略',
    author: {
      email: users[0].email,
      avatar: users[0].avatar,
      name: users[0].username
    },
    date: '2025-05-11 09:00:00 ',
    content: '前端自动化测试的最佳实践...',
    preview: '前端自动化测试的最佳实践...',
    likes: 30,
    view_count: 100,
    comments: []
  },
  {
    id: "12",
    title: 'WebAssembly应用',
    author: {
      email: users[1].email,
      avatar: users[1].avatar,
      name: users[1].username
    },
    date: '2025-05-12 13:00:00 ',
    content: 'WebAssembly在前端的应用场景...',
    preview: 'WebAssembly在前端的应用场景...',
    likes: 36,
    view_count: 100,
    comments: []
  },
  {
    id: "13",
    title: '前端性能优化',
    author: {
      email: users[0].email,
      avatar: users[0].avatar,
      name: users[0].username
    },
    date: '2025-05-13 09:00:00 ',
    content: '前端性能优化的几种常见方法...',
    preview: '前端性能优化的几种常见方法...',
    likes: 28,
    view_count: 100,
    comments: []
  }
];

export const generateBlogList = (params = { page: 1, pageSize: 10 }) => {
  const startIdx = (params.page - 1) * params.pageSize;
  const endIdx = startIdx + params.pageSize;
  const blogs = staticBlogs
    .slice(startIdx, endIdx)
    .map(({ id, title, author, date, preview, view_count, likes }) => ({ id, title, author, date ,preview, view_count, likes}));

  return {
    blogs,
    total: staticBlogs.length
  };
};

export const getSubComments = async (blogId, parentId, page, pageSize) => {
  const comments = staticComment.filter(c => c.parent_id === parentId);
  const startIdx = (page - 1) * pageSize;
  const endIdx = startIdx + pageSize;
  return {
    list: comments.slice(startIdx, endIdx),
    total: comments.length
  };
};

export const getBlogDetail = async (id,  page =  1, pageSize = 10 ) => {
  await new Promise(resolve => setTimeout(resolve, 300));
  const log = staticBlogs.find(b => b.id === id) || {
    id,
    title: `技术文章示例 ${id}`,
    author: {
      email: "default@user.com",
      avatar: "https://randomuser.me/api/portraits/men/1.jpg",
      name: "默认用户"
    },
    date: new Date().toISOString(),
    content: `这是第${id}篇技术文章的完整内容...`,
    likes: Math.floor(Math.random() * 100),
    comments: []
  };
  // const startIdx = (page - 1) * pageSize;
  // const endIdx = startIdx + pageSize;
  // const topLevelComments = staticComment.filter(c => c.parent_id === "");
  const {list, total} = await getSubComments(id, "", page, pageSize);
  log.comments = list;
  return {
      log,
      totalComments: total
    };
};

export const getCommentReplies = async (commentId, params = { page: 1, pageSize: 10 }) => {
  await new Promise(resolve => setTimeout(resolve, 300));
  const blog = staticBlogs.find(b => b.comments.some(c => c.id === commentId));
  if (!blog) return { replies: [], total: 0 };

  const startIdx = (params.page - 1) * params.pageSize;
  const endIdx = startIdx + params.pageSize;
  const replies = blog.comments.filter(c => c.parent_id === commentId);

  return {
    replies: replies.slice(startIdx, endIdx),
    total: replies.length
  };
};

export const submitLike = async (blogId) => {
  await new Promise(resolve => setTimeout(resolve, 300));
  const blog = staticBlogs.find(b => b.id === blogId);
  if (blog) {
    blog.likes += 1;
  }
  return { success: true };
};

export const submitComment = async (blogId, content, parentId = "") => {
  const blog = staticBlogs.find(b => b.id === blogId);
  const user = users[0]; // 使用第一个用户作为当前用户
  
  const comment = {
    id: Date.now().toString(),
    content,
    author: {
      email: user.email,
      avatar: user.avatar,
      name: user.username
    },
    created_at: Date.now(),
    likes: 0,
    parent_id: parentId,
    comment_total: 0
  };

  if (blog) {
    blog.comments.push(comment);
  }

  return {
    success: true,
    comment
  };
};

// 查找评论
function findComment(comments, commentId) {
  return comments.find(comment => comment.id === commentId);
}


export const findBlogById = (blogId) => {
  return staticBlogs.find(blog => blog.id === blogId);
};
